package com.geek.service.impl;

import com.geek.bean.vo.SaveAddressVO;
import com.geek.mapper.MarketAddressMapper;
import com.geek.model.MarketAddress;
import com.geek.model.MarketAddressExample;
import com.geek.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 小豪
 * @version 1.0
 * @description
 * @date 2024-06-17 17:28
 */
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private MarketAddressMapper addressMapper;

    @Override
    public MarketAddress findById(Integer addressId) {
        MarketAddress address = addressMapper.selectByPrimaryKey(addressId);
        return address;
    }
    @Override
    public List<MarketAddress> list(Integer userId) {
        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.createCriteria().andUserIdEqualTo(userId)
                .andDeletedEqualTo(false);
        addressExample.setOrderByClause("add_time desc");
        return addressMapper.selectByExample(addressExample);
    }

    @Override
    public MarketAddress addressDetail(Integer userId, Integer id) {
        MarketAddressExample addressExample = new MarketAddressExample();
        addressExample.createCriteria().andUserIdEqualTo(userId)
                .andDeletedEqualTo(false)
                .andIdEqualTo(id);
        List<MarketAddress> addressList = addressMapper.selectByExample(addressExample);
        return addressList != null && addressList.size() == 1 ? addressList.get(0) : null;
    }

    @Override
    public int deleteById(Integer userId, Integer id) {
        return addressMapper.deleteAddressById(userId, id);
    }

    @Override
    public int update(Integer userId, SaveAddressVO saveAddress) {
        MarketAddress marketAddress = new MarketAddress();
        Date currDate = new Date();
        marketAddress.setUserId(userId);
        marketAddress.setCity(saveAddress.getCity());
        marketAddress.setCounty(saveAddress.getCounty());
        marketAddress.setName(saveAddress.getName());
        marketAddress.setProvince(saveAddress.getProvince());
        marketAddress.setTel(saveAddress.getTel());
        marketAddress.setAddressDetail(saveAddress.getAddressDetail());
        marketAddress.setAreaCode(saveAddress.getAreaCode());
        marketAddress.setUpdateTime(currDate);
        marketAddress.setDeleted(false);
        if (saveAddress.getIsDefault()) {
            MarketAddressExample addressExample = new MarketAddressExample();
            addressExample.createCriteria().andUserIdEqualTo(userId)
                    .andIsDefaultEqualTo(true);
            List<MarketAddress> addressList = addressMapper.selectByExample(addressExample);
            if (addressList.size() != 0 && addressList != null) {
                for (MarketAddress address : addressList) {
                    if (!address.getId().equals(saveAddress.getId())) {
                        address.setIsDefault(false);
                        addressMapper.updateByPrimaryKey(address);
                    }
                }
            }
            marketAddress.setId(saveAddress.getId());
            marketAddress.setIsDefault(saveAddress.getIsDefault());
        } else {
            marketAddress.setId(saveAddress.getId());
            marketAddress.setIsDefault(false);
        }
        addressMapper.updateByPrimaryKey(marketAddress);
        return saveAddress.getId();
    }

    /**
     * 插入新的地址
     */
    @Override
    public Integer save(Integer userId, SaveAddressVO saveAddress) {
        MarketAddress marketAddress = new MarketAddress();
        Date currDate = new Date();
        marketAddress.setUserId(userId);
        marketAddress.setCity(saveAddress.getCity());
        marketAddress.setCounty(saveAddress.getCounty());
        marketAddress.setName(saveAddress.getName());
        marketAddress.setProvince(saveAddress.getProvince());
        marketAddress.setTel(saveAddress.getTel());
        marketAddress.setAddressDetail(saveAddress.getAddressDetail());
        marketAddress.setAreaCode(saveAddress.getAreaCode());
        marketAddress.setUpdateTime(currDate);
        marketAddress.setDeleted(false);
        marketAddress.setAddTime(currDate);
        Boolean isDefault = saveAddress.getIsDefault();
        if (isDefault) {
            MarketAddressExample addressExample = new MarketAddressExample();
            addressExample.createCriteria().andUserIdEqualTo(userId)
                    .andIsDefaultEqualTo(true);
            List<MarketAddress> addressList = addressMapper.selectByExample(addressExample);
            if (addressList.size() != 0 && addressList != null) {
                for (MarketAddress address : addressList) {
                    if (!address.getId().equals(saveAddress.getId())) {
                        address.setIsDefault(false);
                        addressMapper.updateByPrimaryKey(address);
                    }
                }
            }
            marketAddress.setIsDefault(saveAddress.getIsDefault());
            addressMapper.insertSelective(marketAddress);
        } else {
            marketAddress.setIsDefault(false);
            addressMapper.insertSelective(marketAddress);
        }
        return marketAddress.getId();
    }
    @Override
    public List<MarketAddress> findByAId(Integer addressId) {
        MarketAddressExample example = new MarketAddressExample();
        example.createCriteria().andDeletedEqualTo(false)
                .andIdEqualTo(addressId);
        List<MarketAddress> marketAddresses = addressMapper.selectByExample(example);
        return marketAddresses != null && marketAddresses.size() == 1 ? marketAddresses : null;
    }

}
